/**
 * @author zhoushengtao
 * @since 2012-8-27 下午5:43:30
 */

package com.stchou.superbrowser.ui.widget;

import com.stchou.superbrowser.activity.R;
import com.stchou.superbrowser.ui.tag.properties;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SuperTabViewpager extends LinearLayout
{
    private Context m_context;
    private int[] m_titles =
    {
        R.string.app_name, R.string.app_name, R.string.app_name
    };
    private ViewPager m_viewPager;
    private List<View> m_listViews; // Tab页面列表
    private int m_offset = 0;// 动画图片偏移量
    private int m_currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
    private ImageView m_cursor;// 动画图片

    @properties
    private int titleHeight = 50;
    @properties
    private int titleBgColor = Color.WHITE;
    /**
     * @param context
     */
    public SuperTabViewpager(Context context)
    {
        super(context);
        m_context = context;
    }

    public SuperTabViewpager(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        m_context = context;
    }

    public Boolean setRes(int []titles,List<View> listViews)
    {
        if(titles.length != listViews.size())
            return false;
        m_titles = titles;
        m_listViews = listViews;
        init();
        return true;
    }

    
    /**
     * initinalize the layout view
     */
    private void init()
    {
        removeAllViews();
        setBackgroundColor(titleBgColor);
        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        setOrientation(VERTICAL);

        LinearLayout linearLayout = new LinearLayout(m_context);
        linearLayout.setOrientation(HORIZONTAL);
        LayoutParams LiLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LiLayoutParams.height = titleHeight;
        linearLayout.setLayoutParams(LiLayoutParams);

        for (int i = 0; i < m_titles.length; i++)
        {
            TextView textView = new TextView(m_context);
            textView.setText(m_titles[i]);
            LayoutParams lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lParams.weight = 1.0f;
            lParams.gravity = Gravity.CENTER;
            textView.setLayoutParams(lParams);
            textView.setOnClickListener(new MyOnClickListener(i));
            linearLayout.addView(textView);
        }
        addView(linearLayout);

        m_cursor = new ImageView(m_context);
        m_cursor.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        m_cursor.setBackgroundResource(R.drawable.a);
        m_cursor.setScaleType(ScaleType.MATRIX);

        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
                .getWidth();// 获取图片宽度

        m_offset = (480 / m_titles.length - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(m_offset, 0);
        m_cursor.setImageMatrix(matrix);// 设置动画初始位置

        addView(m_cursor);

        LinearLayout l1 = new LinearLayout(m_context);
        l1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        l1.setBackgroundColor(Color.BLUE);
        LinearLayout l2 = new LinearLayout(m_context);
        l2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        l2.setBackgroundColor(Color.RED);
        LinearLayout l3 = new LinearLayout(m_context);
        l3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        l3.setBackgroundColor(Color.YELLOW);

        m_listViews = new ArrayList<View>();
        m_listViews.add(l1);
        m_listViews.add(l2);
        m_listViews.add(l3);

        m_viewPager = new ViewPager(m_context);
        m_viewPager.setPersistentDrawingCache(PERSISTENT_ANIMATION_CACHE);
        m_viewPager.setAdapter(new MyPagerAdapter(m_listViews));
        m_viewPager.setCurrentItem(0);
        m_viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        addView(m_viewPager);

    }

    class MyPagerAdapter extends PagerAdapter
    {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews)
        {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2)
        {
            ((ViewPager)arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0)
        {
        }

        @Override
        public int getCount()
        {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1)
        {
            ((ViewPager)arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1)
        {
        }

        @Override
        public Parcelable saveState()
        {
            return null;
        }

        @Override
        public void startUpdate(View arg0)
        {
        }
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener
    {
        private int index = 0;

        public MyOnClickListener(int i)
        {
            index = i;
        }

        @Override
        public void onClick(View v)
        {
            m_viewPager.setCurrentItem(index);
        }
    };

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements OnPageChangeListener
    {

        int one = m_offset * 2 + bmpW;
        int two = one * 2;

        public void onPageSelected(int arg0)
        {
            Animation animation = null;
            switch (arg0)
            {
                case 0:
                    if (m_currIndex == 1)
                    {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    }
                    else if (m_currIndex == 2)
                    {
                        animation = new TranslateAnimation(two, 0, 0, 0);
                    }
                    break;
                case 1:
                    if (m_currIndex == 0)
                    {
                        animation = new TranslateAnimation(m_offset, one, 0, 0);
                    }
                    else if (m_currIndex == 2)
                    {
                        animation = new TranslateAnimation(two, one, 0, 0);
                    }
                    break;
                case 2:
                    if (m_currIndex == 0)
                    {
                        animation = new TranslateAnimation(m_offset, two, 0, 0);
                    }
                    else if (m_currIndex == 1)
                    {
                        animation = new TranslateAnimation(one, two, 0, 0);
                    }
                    break;
            }
            m_currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            m_cursor.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
        }

        @Override
        public void onPageScrollStateChanged(int arg0)
        {
        }
    }
}
